import router from '@/router/index';
import type { RouteType, viewMenu } from '@/types';
import { isNavigationFailure, NavigationFailureType } from 'vue-router';
import { ElNotification } from 'element-plus';

export const clickMenu = async (menu: viewMenu) => {
    switch (menu.type) {
        case 'tab':
            await routePush(menu.name);
            break;
        case 'link':
            //window.open(menu.path, '_blank')
            break;
        case 'iframe':
            await routePush('', {}, menu.path);
            break;
        default:
            ElNotification({
                message: '导航失败，无法识别菜单类型！',
                type: 'error',
            });
            break;
    }
};

/**
 * 导航失败有错误消息的路由push
 * @param name 路由name
 * @param params 路由参数
 * @param path 路由path,通过path导航无法传递@param params
 */
export const routePush = async (name: string = '', params: any = {}, path: string = '') => {
    try {
        const data = name ? { name: name, state: { params } } : { path: path };
        //console.log(data);
        const failure = await router.push(data);
        if (isNavigationFailure(failure, NavigationFailureType.aborted)) {
            ElNotification({
                message: '导航卫士被拦截',
                type: 'error',
            });
        } else if (isNavigationFailure(failure, NavigationFailureType.duplicated)) {
            // ElNotification({
            //     message: i18n.global.t('utils.Navigation failed, it is at the navigation target position!'),
            //     type: 'warning',
            // })
        }
    } catch (error) {
        const msg = error?.toString();
        if (msg && msg.indexOf('module') >= 0 && msg.indexOf('dynamically') >= 0) {
            ElNotification({
                message: '网页已更新，马上自动刷新网页',
                type: 'warning',
            });
            setTimeout(() => {
                window.location.reload();
            }, 1000);
            return;
        }
        console.log(msg);
        // window.location.reload();
        ElNotification({
            message: '找不到路径文件',
            type: 'error',
        });
    }
};


/**
 * 获取后台菜单的paths
 */
export const getMenuPaths = (menus: any): any[] => {
    let menuPaths: any = [];
    for (const key in menus) {
        if (menus[key].extend == 'add_rules_only') {
            continue;
        }
        menuPaths.push(menus[key] as never);
        if (menus[key].children && menus[key].children.length > 0) {
            menuPaths = menuPaths.concat(getMenuPaths(menus[key].children) as never);
        }
    }
    return menuPaths;
};

/**
 * 动态添加路由-带子路由
 */
export const addRouteAll = (routes: Array<RouteType>, parentName: string) => {
    routes.forEach((item) => {
        if (item.extend == 'add_menu_only') {
            return;
        } else if (item.type == 'menu' && item.menu_type == 'tab' && item.component) {
            addRouteItem(item, parentName);
        }
        if (item.children && item.children.length > 0) {
            addRouteAll(item.children, parentName);
        }
    });
};

/**
 * 动态添加路由
 */
export const addRouteItem = (route: RouteType, parentName: string) => {
    if (parentName) {
        if (route.path) {
            router.addRoute(parentName, {
                path: route.path,
                name: route.name,
                component: route.component,
                props:route.props,
                meta: {
                    title: route.title,
                },
            });
        }
    } else {
        router.addRoute({
            path: '/' + route.path,
            name: route.name,
            props:route.props,
            component: route.component,
            meta: {
                title: route.title,
            },
        });
    }
};
